package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.CountDownButtonHelper;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/17.
 * 修改密码和手机号
 */

public class PasswordChangeActivity extends BaseActivity implements View.OnClickListener ,Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.title_center)
    TextView titleCenter;

    @Bind(R.id.prompting_text)
    TextView promptingText;

    @Bind(R.id.admin_phone)//手机号
    EditText admin_phone;

    @Bind(R.id.admin_btn_yzm)//获取验证
            Button admin_btn_yzm;

    @Bind(R.id.admin_yzm)//输入验证
            EditText admin_yzm;

    @Bind(R.id.password_new_layout)//新密码
    RelativeLayout passwordNewLayout;

    @Bind(R.id.admin_pow)//输入密码
            EditText admin_pow;

    @Bind(R.id.password_new_confirm_Layout)//确认密码
    RelativeLayout passwordNewConfirmLayout;
    @Bind(R.id.confirm_pow)//确认密码
            EditText confirm_pow;
    @Bind(R.id.button)//提交
            Button button;
    Context context;
    private String pos="";//0、修改手机号 1、修改密码 2支付密码
    String Judge_interface="0";//判断哪个接口   0、短信  1、修改手机号 或 修改密码 或 支付密码
    private MyProgressDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_change);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        if (getIntent()!=null){
            pos=getIntent().getStringExtra("pos");
        }
        if (pos.equals("0")){
            passwordNewLayout.setVisibility(View.GONE);
            passwordNewConfirmLayout.setVisibility(View.GONE);
            titleCenter.setText(getResources().getString(R.string.phone_number));
            promptingText.setText(getResources().getString(R.string.phone_number_text));
        }else  if (pos.equals("2")){
            titleCenter.setText(getResources().getString(R.string.pay_pas));
        }
        retur.setOnClickListener(this);
        button.setOnClickListener(this);
        admin_btn_yzm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.retur:
                finish();
                break;
            case R.id.admin_btn_yzm://短信
                if (TextUtils.isEmpty(admin_phone.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(admin_phone.getText().toString().trim())){//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }
                Judge_interface="0";
                setPhonenumber();
                break;
            case R.id.button://注册
                if (TextUtils.isEmpty(admin_phone.getText().toString().trim())){//手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(admin_phone.getText().toString().trim())){//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }

                if (TextUtils.isEmpty(admin_yzm.getText().toString().trim())){//验证码
                    ToastUtils.show(context, getResources().getString(R.string.please_verification_code));
                    return;
                }
                if (!pos.equals("0")){
                if (TextUtils.isEmpty(admin_pow.getText().toString().trim())){//密码
                    ToastUtils.show(context, getResources().getString(R.string.please_login_password));
                    return;
                }
                if (admin_pow.getText().toString().trim().length()<6){//密码大于6位
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_login_password));
                    return;
                }
                if (TextUtils.isEmpty(confirm_pow.getText().toString().trim())){//确认密码
                    ToastUtils.show(context, getResources().getString(R.string.please_again_login_password));
                    return;
                }
                if (!admin_pow.getText().toString().trim().equals(confirm_pow.getText().toString().trim())){//两次密码对比
                    ToastUtils.show(context, getResources().getString(R.string.two_passwords_are_different));
                    return;
                }
                }
                Judge_interface="1";
                setPhonenumber();
                break;
        }
    }
    //提交短信 和注册
    public void setPhonenumber(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            if (Judge_interface.equals("0")){
                String post ="4" ;
                 if(pos.equals("0")){
                     post ="4" ;
                 }else if(pos.equals("1")){
                     post ="7" ;
                 }else if(pos.equals("2")){
                     post ="8" ;
                 }
                HttpClient.note(admin_phone.getText().toString().trim(), post, this);
            }else {
                if (pos.equals("0")){
                    HttpClient.editPhone(admin_phone.getText().toString().trim(), admin_yzm.getText().toString().trim(), this);
                }else  if (pos.equals("1")){
                    HttpClient.resetLoginPass(admin_phone.getText().toString().trim(),admin_pow.getText().toString().trim(), admin_yzm.getText().toString().trim(), this);
                }else {
                 HttpClient.editPaymentPassword(admin_pow.getText().toString().trim(), admin_yzm.getText().toString().trim(), this);
                }
                }
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }

    }
    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        List<RegistrationModel> getnote = JsonTools.getnote(result);
        System.out.println("stringmd5result"+result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                if (Judge_interface.equals("0")){
                    // 验证码
                    CountDownButtonHelper helper = new CountDownButtonHelper(admin_btn_yzm, 120, 1);
                    helper.start();
                    ToastUtils.show(context, getMsg);
                }else {
                    if(pos.equals("0")){
                        UserInfoUtils.getInstans().updatephone(context,admin_phone.getText().toString().trim());
                    }
                    finish();
                    ToastUtils.show(context, getMsg);
                }
            }else {
                ToastUtils.show(context, getMsg);
            }
        }
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        //ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
       // ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}


